package medicalDocumentation.medicalDoc;

import entity.MedicalCase;
import entity.MedicalProcedure;
import entity.PlanProcedure;
import entity.Treatment;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateful
public class CaseTreatmentSessionBean implements CaseTreatmentSessionBeanRemote {

    @PersistenceContext
    private EntityManager em;
    private Treatment treatment;
    private MedicalCase medicalCase;
    private List<Treatment> res;

    public CaseTreatmentSessionBean() {
    }

    @Override
    public List<PlanProcedure> findProcedure(Long treatmentID) {
        treatment = em.find(Treatment.class, treatmentID);
        return treatment.getProcedureList();
    }

    
//    public void UpdateProcedureStatus(String treatmentID,String procedureID){
//        PlanProcedure procedure=em.find(PlanProcedure.class, procedureID);
//        
//        Treatment treatment=em.find(Treatment.class, treatmentID);
//        List<PlanProcedure> planProcedureList=new ArrayList();
//   
//        procedure.setStatus("Ordered");
// 
//        
//        em.persist(procedure);
//        
//    }
    @Override
    public List<Treatment> displayCaseTreatment(Long CIN) {
        MedicalCase mCase = em.find(MedicalCase.class, CIN);
        res = new ArrayList();

        for (int i = 0; i < mCase.getTreatment().size(); i++) {
            res.add(mCase.getTreatment().get(i));

        }
        return res;
    }

    @Override
    public Long addTreatment(Long CIN, Date date, String status, String authDoctor, String consultant) {
        treatment = new Treatment(date, authDoctor, consultant, status);

//            res.add(diseaseEntity);
//          System.out.println("Session bean ----- add Medicine called" + medicineEntity.getMedicineName()+ medicineEntity.getCode()+ medicineEntity.getDose());
//      
//             System.out.println("Session bean ----- add Medicine called" +CIN);
        medicalCase = em.find(MedicalCase.class, CIN);

//             System.out.println("Session bean ----- add Medicine called" + medicalCase.getCIN());
//        System.out.println("Session bean ----- add Medicine called" + medicineEntity.getCode());

        List<Treatment> tl = medicalCase.getTreatment();
        tl.add(treatment);
        medicalCase.setTreatment(tl);
        //     medicalCase.setMedicines(medicineEntityList);
//             
        em.persist(treatment);

        em.flush();
        return treatment.getId();
    }
    
    @Override
    public void deleteTreatment(Long CIN, Long ID) {
        Treatment medicine = em.find(Treatment.class, ID);
        MedicalCase mCase = em.find(MedicalCase.class, CIN);
        List<Treatment> tl = mCase.getTreatment();
        tl.remove(medicine);
        
        mCase.setTreatment(tl);

        em.remove(medicine);
        em.flush();
    }
    
    @Override
    public void updateTreatment(Treatment treatment){
        em.merge(treatment);
        em.flush();
    }
}
